<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        export default (arr)=>{
    function maximumItemSum(array,leftest,rightest){
    //递归只剩下一项
    if(leftest == rightest) {  
      return array[rightest]>0?array[rightest]:0
    }else{
      let middle = Math.floor((leftest+rightest)*0.5); //相除之后向下取整,求得中间分割线
      //左侧的最大子序列  
      let maxLeftSum = maximumItemSum(arr, leftest, middle);  
      //右侧的最大子序列
      let maxRightSum = maximumItemSum(arr, middle+1, rightest);  

      //中线左边求和。
      let sumLeftTmp = 0; //暂存左边累加的结果
      let maxBorderLeft = 0;  //用于存储分割线左边和的最大值
      for(let i=middle;i>=leftest;i--){
        sumLeftTmp +=array[i];
        if(sumLeftTmp>maxBorderLeft){
          maxBorderLeft = sumLeftTmp;
        }
      }

      //中线右边求和。
      let sumRightTmp = 0;//暂存右边累加的结果
      let maxBorderRight = 0; //用于存储分割线右边的最大值
      for(let j=middle+1;j<=rightest;j++){
        sumRightTmp +=array[j];
        if(sumRightTmp>maxBorderRight){
          maxBorderRight =sumRightTmp;
        }
      }

      //取出最大的值
      let maxSum = Math.max(maxLeftSum,maxRightSum,maxBorderLeft+maxBorderRight);
      return maxSum
    }      
  }
  console.log('maxSum:',maximumItemSum(arr,0,arr.length-1));
  return maximumItemSum(arr,0,arr.length-1);
}
    </script>
</head>
<body>
    <div>输入:-2,11,-4,13,-5,-2</div>
    <div>输出：20</div>
</body>
</html>